package com.xianyun.book.event.handler;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.alibaba.cola.dto.Response;
import com.xianyun.book.common.core.event.EventHandler;
import com.xianyun.book.common.core.event.EventHandlerI;
import com.xianyun.book.domain.order.OrderStatus;
import com.xianyun.book.domain.pay.PayStatus;
import com.xianyun.book.dto.domainevent.OrderBuyCreateEvent;
import com.xianyun.book.gatewayimpl.database.OrderMapper;
import com.xianyun.book.gatewayimpl.database.dataobject.OrderDO;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 买家订单创建事件
 * @author ytt
 */
@CatchAndLog
@Slf4j
@EventHandler
public class OrderBuyCreateHandler implements EventHandlerI<Response, OrderBuyCreateEvent> {
    @Resource
    private OrderMapper orderMapper;

    @Override
    public Response execute(@Valid OrderBuyCreateEvent createEvent) {
        log.info("获取卖方预售订单编号:{}----正在处理",createEvent.getRelateOrderId());
        OrderDO relateOrder = orderMapper.getOrderInfoById(createEvent.getRelateOrderId());
        PayStatus payStatus = PayStatus.getPayStatusById(createEvent.getPayStatus());
        switch (payStatus){
            case WAIT_CONFIRM:{
                relateOrder.setOrderStatus(OrderStatus.WAIT_CONFIRM.getStatusId());
                break;
            }
            case FINISH:{
                relateOrder.setOrderStatus(OrderStatus.HAVE_FINISH.getStatusId());
                break;
            }
            default:{
                break;
            }
        }
        orderMapper.updateOrder(relateOrder);
        log.info("卖方预售订单编号:{}----处理完成,当前状态:{}",createEvent.getRelateOrderId(),payStatus.getMessage());
        return Response.buildSuccess();
    }
}
